package com.acme.entity.util;

import java.util.ArrayList;
import java.util.Collection;
import javax.swing.event.ChangeListener;

public class ChangeSupportList<E> extends ArrayList<E> {

    protected ChangeSupport support = new ChangeSupport(this);

    public ChangeSupportList() {
    }

    public ChangeSupportList(Collection<? extends E> elements) {
        super(elements);
    }

    public void addChangeListener(ChangeListener listener) {
        support.addChangeListener(listener);
    }

    public void removeChangeListener(ChangeListener listener) {
        support.removeChangeListener(listener);
    }

    @Override
    public boolean add(E object) {
        boolean result = super.add(object);
        if (result) support.fireChange();
        return result;
    }

    @Override
    public void add(int index, E element) {
        super.add(index, element);
        support.fireChange();
    }

    @Override
    public void clear() {
        super.clear();
        support.fireChange();
    }

    @Override
    public E remove(int index) {
        E obj = super.remove(index);
        if(obj!=null) support.fireChange();
        return obj;
    }

    @Override
    public boolean remove(Object o) {
        boolean result = super.remove(o);
        if (result) support.fireChange();
        return result;
    }

    @Override
    public E set(int index, E element) {
        E obj = super.set(index, element);
        if(obj!=null) support.fireChange();
        return obj;
    }

    @Override
    protected void removeRange(int fromIndex, int toIndex) {
        super.removeRange(fromIndex, toIndex);
        support.fireChange();
    }

}
